package crmdna.client.program;

import java.util.List;
import java.util.Set;

import javax.annotation.Nullable;
import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

import crmdna.client.practice.Practice;
import crmdna.client.practice.Practice.PracticeProp;
import crmdna.client.program.Program.ProgramProp;
import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.Utils;
import crmdna.crmdna.Client;

@Api(name = "program")
public class ProgrameApi {
	@ApiMethod(path="create", httpMethod = HttpMethod.POST)
	public APIResponse create(@Named("client") String client, 
			@Named("groupId") long groupId, @Named("programTypeId") long programTypeId,
			@Named("venueId") long venueId, @Named("teacherId") long teacherId,
			@Named("startYYYYMMDD") int startYYYYMMDD, @Named("endYYYYMMDD") int endYYYYMMDD,
			@Named("numBatches") int numBatches, 
			@Nullable @Named("description") String description, User user) {
		
		try {
			ProgramProp programProp = Program.create(client, groupId, programTypeId, 
					venueId, teacherId, startYYYYMMDD, endYYYYMMDD, 
					numBatches, description, Utils.getLoginEmail(user));
			
								
			return new APIResponse(Status.SUCCESS, programProp);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}			
	}
	
	@ApiMethod(path="query", httpMethod = HttpMethod.GET)
	public APIResponse query(@Named("client") String client, 
			@Nullable @Named("startYYYYMMDD") Long startYYYYMMDD,
			@Nullable @Named("endYYYYMMDD") Long endYYYYMMDD,
			@Nullable @Named("programTypeId") Long programTypeId,
			@Nullable @Named("groupIds") Set<Long> groupIds, 
			@Nullable @Named("venueId") Long venueId, 
			@Nullable @Named("limit") Integer limit) {
		
		try {
			List<ProgramProp> programProps = Program.query(client, startYYYYMMDD, endYYYYMMDD, 
					programTypeId, groupIds, venueId, limit); 
												
			return new APIResponse(Status.SUCCESS, programProps);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}			
	}
	
	@ApiMethod(path="update", httpMethod = HttpMethod.POST)
	public APIResponse update(@Named("client") String client,
			@Named("programId") long programId,
			@Nullable @Named("newVenueId") long newVenueId, 
			@Nullable @Named("newTeacherId") long newTeacherId,
			@Nullable @Named("newStartYYYYMMDD") int newStartYYYYMMDD, 
			@Nullable @Named("newEndYYYYMMDD") int newEndYYYYMMDD,
			@Nullable @Named("newNumBatches") int newNumBatches, 
			@Nullable @Named("newDescription") String newDescription, 
			User user) {
		
		try {
			ProgramProp programProp = Program.update(client, programId, 
					newVenueId, newTeacherId, newStartYYYYMMDD, newEndYYYYMMDD, 
					newNumBatches, newDescription, Utils.getLoginEmail(user));
										
			return new APIResponse(Status.SUCCESS, programProp);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}			
	}
	
	@ApiMethod(path="rename", httpMethod = HttpMethod.GET)
	public APIResponse rename(@Named("client") String client, 
			@Named("practiceId") long practiceId, 
			@Named("newDisplayName") String newDisplayName, User user) {
		
		try {
			Client.ensureValidClient(client);
			PracticeProp prop = Practice.rename(client, practiceId, newDisplayName, Utils.getLoginEmail(user));
			
			return new APIResponse(Status.SUCCESS, prop);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="delete", httpMethod = HttpMethod.GET)
	public APIResponse delete(@Named("client") String client, 
			@Named("practiceId") long practiceId, User user) {
		
		try {
			Client.ensureValidClient(client);
			Practice.delete(client, practiceId, Utils.getLoginEmail(user));
			
			return new APIResponse(Status.SUCCESS, "Center [" + practiceId + "] deleted");			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}		
}
